package com.zhengtoudai.ui.presenter;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.zhengtoudai.http.HttpApi;
import com.zhengtoudai.injector.PerActivity;
import com.zhengtoudai.ui.bean.AccountLog;
import com.zhengtoudai.ui.view.AccountLogView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import rx.Observer;
import rx.functions.Action0;

/**
 * Created by Administrator on 2016/8/29 0029.
 */
@PerActivity
public class AccountLogPresenterImpl extends BasePresenterImpl<AccountLogView> implements AccountLogPresenter {

    @Inject
    public AccountLogPresenterImpl(){

    }

    @Override
    public void accountLog(Map<String, String> params) {
        mSubscription=HttpApi.getInstance().accountLog(params).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                mView.showDialog();
            }
        }).subscribe(new Observer<JsonObject>() {
            @Override
            public void onCompleted() {
                mView.dismissDialog();
            }

            @Override
            public void onError(Throwable e) {
                mView.onFailure(e);
                mView.dismissDialog();
            }

            @Override
            public void onNext(JsonObject result) {
                AccountLog accountLog=new AccountLog();
                accountLog.setCode(result.get("code").toString());
                accountLog.setMessage(result.get("message").toString());
                try {
                    JSONObject resultObj=new JSONObject(result.get("result").toString());
                    AccountLog.ResultEntity resultEntity=new AccountLog.ResultEntity();
                    if (resultObj.has("pageInfo")){
                        AccountLog.ResultEntity.PageInfoEntity pageInfoEntity=new Gson().fromJson(resultObj.optJSONObject("pageInfo").toString(),AccountLog.ResultEntity.PageInfoEntity.class);
                        resultEntity.setPageInfo(pageInfoEntity);
                    }
                    List<AccountLog.ResultEntity.ListEntity> listEntity=
                            new Gson().fromJson(resultObj.optString("list")
                                    ,new TypeToken<List<AccountLog.ResultEntity.ListEntity>>(){}.getType());
                    resultEntity.setList(listEntity);
                    if (resultObj.has("LOG_ALL_TRADE_CODE")){
                        JSONObject tradeCode=resultObj.optJSONArray("LOG_ALL_TRADE_CODE").optJSONObject(0);
                        Iterator it = tradeCode.keys();
                        List<AccountLog.ResultEntity.TradeCode> tradeCodes=new ArrayList<>();
                        while (it.hasNext()){
                            String key= (String) it.next();
                            String value=tradeCode.optString(key);
                            AccountLog.ResultEntity.TradeCode tradeCode1=new AccountLog.ResultEntity.TradeCode(key,value);
                            tradeCodes.add(tradeCode1);
                        }
                        resultEntity.setTradeCodes(tradeCodes);

                    }
                    accountLog.setResult(resultEntity);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mView.onResult(accountLog);
            }
        });
    }
}
